package com.whmskj.xjlhbc.mobile.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.Bcsc15sccyjlb;
import com.whmskj.xjlhbc.domain.Bcsc15sctz;
import com.whmskj.xjlhbc.domain.PersonnelManagementDataSheets;
import com.whmskj.xjlhbc.mapper.Bcsc15sccyjlbMapper;
import com.whmskj.xjlhbc.mapper.Bcsc15sctzMapper;
import com.whmskj.xjlhbc.service.IPersonnelManagementDataSheetsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Tag(name = "生产通知",description = "生产通知")
@RequestMapping("/sctz")
public class SctzController {

    @Autowired
    private Bcsc15sctzMapper bcsc15sctzMapper;

    @Autowired
    private Bcsc15sccyjlbMapper bcsc15sccyjlbMapper;

    @Autowired
    private IPersonnelManagementDataSheetsService personnelManagementDataSheetsService;

    /**
     * 生产通知列表
     */
    @GetMapping("/tzList")
    @Operation(description = "生产通知列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult zjList(){
        Page.startPage();
        LambdaQueryWrapper<Bcsc15sctz> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Bcsc15sctz::getReleaseTime);
        List<Bcsc15sctz> list = bcsc15sctzMapper.selectList(wrapper);
        return Page.getDataTable(list);
    }

    @GetMapping("/tzDetail/{id}")
    @Operation(description = "生产通知详情")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult tzDetail(@PathVariable Long id){
        Bcsc15sctz bcsc15sctz = bcsc15sctzMapper.selectById(id);
        Bcsc15sccyjlb bcsc15sccyjlb = new Bcsc15sccyjlb();
        PersonnelManagementDataSheets personnelManagementDataSheets = personnelManagementDataSheetsService.getByLoginUserId();
        String fe1e = personnelManagementDataSheets.getFe1e();
        LambdaQueryWrapper<Bcsc15sccyjlb> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bcsc15sccyjlb::getGlzbid,id);
        wrapper.eq(Bcsc15sccyjlb::getCyname, fe1e);
        Long count = bcsc15sccyjlbMapper.selectCount(wrapper);
        if(Objects.isNull(count) || count==0) {
            bcsc15sccyjlb.setCyname(fe1e);
            bcsc15sccyjlb.setPositions(personnelManagementDataSheets.getPositions1());
            bcsc15sccyjlb.setAccessTime(DateUtil.now());
            bcsc15sccyjlb.setGlzbid(id);
            bcsc15sccyjlbMapper.insert(bcsc15sccyjlb);
        }
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bcsc15sccyjlb::getGlzbid,id);
        wrapper.orderByDesc(Bcsc15sccyjlb::getAccessTime);
        List<Bcsc15sccyjlb> cyjlList = bcsc15sccyjlbMapper.selectList(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tzDetail", bcsc15sctz);
        map.put("cyjlList", cyjlList);
        return AjaxResult.success(map);
    }


}
